// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![allow(unused_imports, unused_variables, dead_code)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::thread;
use std::time::Duration;
use log::{error, info, LevelFilter};
use tauri::{Manager, Window};
use crate::config_util::{get_config, get_excel_path_mem, set_excel_path, set_excel_path_mem, set_save_path};
use crate::excel_util::{get_sheets_id_numbers, save_excel_file};
use crate::model::{Config, SheetIdNumber};

pub(crate) mod excel_util;
pub(crate) mod config_util;
pub(crate) mod model;

#[tauri::command]
async fn update_excel_path(excel_path: &str) -> Result<String, String> {
    info!("update excel path: {}", excel_path);
    // set_excel_path(excel_path.to_string());
    set_excel_path_mem(excel_path.to_string());
    Ok(excel_path.to_string())
}

#[tauri::command]
async fn get_excel_path() -> String {
    // let config_option = get_config();
    // match config_option {
    //     None => {
    //         String::new()
    //     }
    //     Some(config) => {
    //         let excel_path_option = config.excel_path;
    //         match excel_path_option {
    //             None => {
    //                 String::new()
    //             }
    //             Some(excel_path) => {
    //                 excel_path
    //             }
    //         }
    //     }
    // }
    get_excel_path_mem()
}

#[tauri::command]
async fn read_excel() -> Vec<SheetIdNumber> {
    let excel_path = get_excel_path_mem();
    if excel_path.len() > 0 {
        return get_sheets_id_numbers(excel_path.as_str());
    }
    Vec::new()
}

// 保存修改后的文件
#[tauri::command]
async fn save_excel(path: &str, sheet_id_numbers: Vec<SheetIdNumber>, prefix: usize, suffix: usize, symbol: &str) -> Result<String, String> {
    set_save_path(path.to_string());
    save_excel_file(sheet_id_numbers, prefix, suffix, symbol)
}

#[tauri::command]
async fn close_splashscreen(window: tauri::Window) {
    if let Some(splashscreen) = window.get_window("splashscreen") {
        thread::sleep(Duration::from_secs(3));
        let result = splashscreen.close();
        if let Err(err) = result {
            error!("close splashscreen error: {:?}", err);
        }
    }
    let option = window.get_window("main");
    if let Some(main) = option {
        let show = main.show();
        if let Err(err) = show {
            error!("show main window error: {:?}", err);
        }
        main.set_focus().unwrap();
        main.set_resizable(false).unwrap();
        main.set_decorations(true).unwrap();
    }
}

fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
    let splashscreen_window_option = app.get_window("splashscreen");
    if let Some(splashscreen_window) = splashscreen_window_option {
        let cursor_visible = splashscreen_window.set_cursor_visible(true);
        if let Err(err) = cursor_visible {
            error!("cursor visible error: {:?}", err);
        }
    }
    Ok(())
}

fn main() {
    env_logger::builder().filter_level(LevelFilter::Info).init();

    let tauri_init_result = tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![close_splashscreen,update_excel_path,get_excel_path,read_excel,save_excel])
        .setup(setup)
        .run(tauri::generate_context!());
    if let Err(err) = tauri_init_result {
        error!("error while running tauri application: {:?}", err);
    }
}

#[test]
pub fn test() {
    set_excel_path("C:/Users/Xinyu/Desktop".to_string());
}
